import { message } from 'ant-design-vue'
import moment from 'moment'

export const timeFormat = (time: string | number) =>
  moment(Number(time)).format('YYYY-MM-DD HH:mm:ss')

export const timeFormat_2 = (time: string | number) => {
  if (time != '0') {
    return moment(Number(time)).format('YYYY-MM-DD HH:mm:ss')
  } else {
    return '-'
  }
}

export const timeFormatYMD = (time: string | number) => moment(Number(time)).format('YYYY-MM-DD')

// 时间戳
export const timeStamp = (time: string | number) => moment(time).valueOf()

export const getNowTime = () => moment(moment().format()).format('YYYY-MM-DD HH:mm:ss')

export const getNowDay = () => moment(moment().format()).format('YYYY-MM-DD')

export const toYuan = (num: number): string => {
  let money = String(num)

  // if (money.length === 1) {
  //   money = '0.0' + money
  // } else if (money.length === 2) {
  //   money = '0.' + money
  // } else if (money.length >= 3) {
  //   money = money.slice(0, money.length - 2) + '.' + money.slice(-2)
  // }

  if (money.startsWith('-')) {
    // 移除负号，先处理数字部分，最后再添加负号
    money = money.slice(1)
    if (money.length === 1) {
      money = '-0.0' + money
    } else if (money.length === 2) {
      money = '-0.' + money
    } else if (money.length >= 3) {
      money = '-' + money.slice(0, money.length - 2) + '.' + money.slice(-2)
    }
  } else {
    if (money.length === 1) {
      money = '0.0' + money
    } else if (money.length === 2) {
      money = '0.' + money
    } else if (money.length >= 3) {
      money = money.slice(0, money.length - 2) + '.' + money.slice(-2)
    }
  }

  return money
}

export function regYuanToFen(yuan: number | string, digit: number) {
  var m = 0
  var s1 = yuan.toString()
  var s2 = digit.toString()
  try {
    m += s1.split('.')[1].length
  } catch (e) { }
  try {
    m += s2.split('.')[1].length
  } catch (e) { }
  return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m)
}

export function copyToCliboard(text: string) {
  if (navigator.clipboard) {
    navigator.clipboard.writeText(text)
  } else {
    let textarea = document.createElement('textarea')
    textarea.value = text
    document.body.appendChild(textarea)
    textarea.select()
    textarea.setSelectionRange(0, textarea.value.length)
    document.execCommand('copy')
    document.body.removeChild(textarea)
  }
  message.success('复制成功！')
}

export function findParentsByChildValue(data: any, targetValue: any) {
  function traverse(items: any, parent = null) {
    for (let item of items) {
      if (item.children) {
        // 递归检查子项
        const result: any = traverse(item.children, item)
        if (result) {
          return result
        }
      }

      // 如果当前项没有子项或者子项中没有找到，检查当前项的值
      if (item.value == targetValue) {
        // 如果找到了目标值，返回前两级的label和value
        return {
          level1Label: parent ? parent.label : null,
          level1Value: parent ? parent.value : null,
          level2Label: item.label,
          level2Value: item.value,
          level3Value: item.value, // 因为已经找到了第三级，所以直接返回
        }
      }
    }
    return null // 如果没有找到，返回null
  }

  return traverse(data) // 从根级别开始遍历
}

//判断时间段是否重叠交叉
export function areIntervalsNonOverlapping(intervals: any) {
  // 辅助函数：将时间字符串转换为从当天午夜开始计算的分钟数
  function convertToMinutes(timeString: any) {
    const [hours, minutes, seconds] = timeString.split(':').map(Number)
    return hours * 60 + minutes // 忽略秒数，如果需要更高精度可以加上 seconds
  }

  // 转换时间并排序
  const sortedIntervals = intervals
    .map(interval => ({
      ...interval,
      beginMinutes: convertToMinutes(interval.polishBeginTime),
      endMinutes: convertToMinutes(interval.polishEndTime),
    }))
    .sort((a, b) => a.beginMinutes - b.beginMinutes)

  // 检查重叠
  for (let i = 1; i < sortedIntervals.length; i++) {
    const current = sortedIntervals[i]
    const previous = sortedIntervals[i - 1]

    // 处理跨越午夜的情况
    if (
      current.beginMinutes < previous.endMinutes ||
      (current.beginMinutes === 0 && previous.endMinutes > 23 * 60)
    ) {
      return false // 重叠
    }
  }

  return true // 没有重叠
}

export function parseTimeToMinutes(timeString) {
  const [hours, minutes, seconds] = timeString.split(':').map(Number)
  return hours * 60 + minutes + seconds / 60
}

export function checkDeviceType() {
  const userAgent = navigator.userAgent || navigator.vendor || window.opera

  // 使用单个正则表达式匹配移动设备
  const mobileRegex = /windows phone|android|ipad|iphone|ipod/i

  // 检查用户代理是否匹配任何移动设备
  if (mobileRegex.test(userAgent)) {
    if (/windows phone/i.test(userAgent)) {
      return 'Windows Phone'
    }
    if (/android/i.test(userAgent)) {
      return 'Android'
    }
    return 'iOS' // 包括 iPad、iPhone 和 iPod
  }

  // 默认返回桌面设备
  return 'Desktop'
}
